package ru.pb.game;

import org.apache.log4j.Logger;

import ru.pb.game.avto_save.AutoSaveRunner;
import ru.pb.game.avto_save.GameShutdownThread;
import ru.pb.game.configs.GameThreadingConfig;
import ru.pb.game.controllers.AuthServerController;
import ru.pb.game.data.loaders.GAMEConfigLoader;
import ru.pb.game.data.loaders.GAMEParsersLoader;
import ru.pb.game.data.scripts.ScriptLoader;
import ru.pb.game.data.scripts.annotations.OnLoad;
import ru.pb.game.data.zip.ZipDataCache;
import ru.pb.game.handlers.AdminCommandHandler;
import ru.pb.game.handlers.ChatHandler;
import ru.pb.game.idfactory.IdFactory;
import ru.pb.game.network.GNetworkEngine;
import ru.pb.game.network.threading.DeadlockDetector;
import ru.pb.game.network.threading.ThreadPoolManager;
import ru.pb.game.objects.storage.ObjectsStorage;
import ru.pb.global.dao.DAOManager;
import ru.pb.global.dbc.DatabaseFactory;
import ru.pb.global.info.PrintInfo;
import ru.pb.global.logging.LoggingService;
import ru.pb.global.memory.GCTask;

/**
 * @author Felixx
 */
public class GameServer
{
	private static final Logger _log = Logger.getLogger(GameServer.class);

	public static void main(String... args) throws Exception
	{
		PrintInfo.getInstance();

		LoggingService.load();

		PrintInfo.printLines("Loadig configs", "Start");
		GAMEConfigLoader.loadAll();
		PrintInfo.printLines("Loadig configs", "Done");
		PrintInfo.printEmptyLine();

		PrintInfo.printLines("Init DataBase", "Start");
		DatabaseFactory.getInstance();
		PrintInfo.printLines("Init DataBase", "Done");
		PrintInfo.printEmptyLine();

		IdFactory.getInstance();
		ThreadPoolManager.getInstance();

		ObjectsStorage.getInstance();

		if(GameThreadingConfig.DEADLOCK_INTERVAL > 0)
		{
			DeadlockDetector.getInstance().start();
		}
		AutoSaveRunner.getInstance();

		PrintInfo.printLines("Hard Tasks", "Start");
		DAOManager.getInstance().load("game");
		ScriptLoader.getInstance();
		ZipDataCache.getInstance();
		PrintInfo.printLines("Hard Tasks", "Done");
		PrintInfo.printEmptyLine();

		PrintInfo.printLines("Load XML", "Start");
		GAMEParsersLoader.parseAll();
		PrintInfo.printLines("Load XML", "Done");
		PrintInfo.printEmptyLine();

		ScriptLoader.getInstance().invoke(OnLoad.class);

		PrintInfo.printLines("Handlers", "Start");
		AdminCommandHandler.getInstance().log();
		ChatHandler.getInstance().log();
		PrintInfo.printLines("Handlers", "Done");
		PrintInfo.printEmptyLine();

		AuthServerController.getInstance();

		PrintInfo.printLines("Init Network engine", "Start");
		GNetworkEngine.getInstance();
		PrintInfo.printLines("Init Network engine", "Done");
		PrintInfo.printEmptyLine();

		AutoSaveRunner.getInstance().start();
		ZipDataCache.getInstance().clear();

		PrintInfo.printLines("ObjectsStorage", "Info");
		ObjectsStorage.getInstance().info();
		PrintInfo.printEmptyLine();

		PrintInfo.printLibInfo(_log);
		PrintInfo.printEmptyLine();
		PrintInfo.getInstance().info();
		ThreadPoolManager.getInstance().scheduleAtFixedRate(new GCTask(), 60 * 1000 * 60, 60 * 1000 * 60); // gc() каждый час.
		Runtime.getRuntime().addShutdownHook(new GameShutdownThread()); // Ловим трид остановки сервера.
	}
}